from litemall_0805.frame.utils.log_utils import logger
from litemall_0805.frame.utils.utils import repeat_click
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    """
    基类：基础方法的封装
    """

    def __init__(self, base_driver=None):

        if base_driver:
            self.driver = base_driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
            self.driver.implicitly_wait(3)
            self.driver.get("https://litemall.hogwarts.ceshiren.com/#/login")

    def do_quit(self):
        """
        退出浏览器
        :return:
        """
        self.driver.quit()

    def do_find(self, by, locate=None):
        """
        元素查找方法的二次封装
        :param by: 定位方法
        :param locate: 元素
        :return:
        """
        if locate:
            logger.info(f"查找元素:{locate}")
            return self.driver.find_element(by, locate)
        else:
            logger.info(f"查找元素:{by}")
            return self.driver.find_element(*by)

    def do_click(self, by, locate=None):
        """
        元素点击方法的二次封装
        :param by: 元素定位方法
        :param locate: 元素
        :return:
        """
        if locate:
            logger.info(f"点击元素:{locate}")
            self.driver.find_element(by, locate).click()
        else:
            logger.info(f"点击元素:{by}")
            self.driver.find_element(*by).click()

    def do_send_keys(self, value, by, locate=None):
        """
        在输入框中输入值的方法的二次封装
        :param value: 输入值
        :param by: 元素定位方法
        :param locate: 元素
        :return:
        """
        if locate:
            logger.info(f"向元素{locate}，输入值：{value}")
            self.driver.find_element(by, locate).clear()
            self.driver.find_element(by, locate).send_keys(value)
        else:
            logger.info(f"向元素{by}，输入值：{value}")
            self.driver.find_element(*by).clear()
            self.driver.find_element(*by).send_keys(value)

    def wait_visible_locate(self, by, locate=None):
        """
        对等待元素可见方法的二次封装
        :param by: 元素定位方法
        :param locate: 元素
        :return:
        """
        if locate:
            logger.info(f"查找元素:{locate}")
            return WebDriverWait(self.driver, 10).until(
                expected_conditions.visibility_of_element_located((by, locate)))
        else:
            logger.info(f"查找元素:{by}")
            return WebDriverWait(self.driver, 10).until(
                expected_conditions.visibility_of_element_located((by)))

    def wait_repeat_click(self, by, locate=None):
        """
        重复点击功能二次改造
        :param by: 定位方法
        :param locate: 定位元素
        :return:
        """
        if locate:
            logger.info(f"点击元素:{locate}")
            WebDriverWait(self.driver, 10).until(repeat_click(by, locate))
        else:
            logger.info(f"点击元素:{by}")
            WebDriverWait(self.driver, 10).until(repeat_click(by))
